package com.situ.stl.controller;

import com.situ.stl.entity.Coach;
import com.situ.stl.entity.Result;
import com.situ.stl.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** 
 * Controller层 - 教练
 */
@RestController 
@RequestMapping( value = "/coach")
public class CoachController{

    /** 
     */
    @Autowired 
    private CoachService coachService;

    /** 
     * add
     */
    @PostMapping 
    public Result add(@RequestBody Coach coach) {
        try {
            coachService.add(coach);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /** 
     * remove
     */
    @DeleteMapping ("/{id}")
    public Result remove(@PathVariable("id") Integer id) {
        try {
            coachService.remove(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /** 
     * edit
     */
    @PutMapping 
    public Result edit( @RequestBody Coach coach) {
        try {
            coachService.edit(coach);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /** 
     * getAll
     */
    @GetMapping 
    public Result getAll( Integer page, Integer limit, Coach coach) {
        if ( page == null && limit == null) {
        	return Result.success(coachService.search(coach));
        } else {
        	return Result.success(coachService.searchByPage(page, limit, coach));
        }
    }

    /** 
     * getById
     */
    @GetMapping( value = "/{id}") 
    public Result getById( @PathVariable( value = "id") Integer id) {
        return Result.success(coachService.getById(id));
    }

}